import { Effect, history } from 'umi';
import * as service from '@/service/index';
import { message, Button, Space } from 'antd';
import { setCookie } from '@/utils';
//todo dva的数据块

interface ILoginModel {
  namespace: string;
  effects: {
    getLogin: Effect;
  };
}
const LoginModel: ILoginModel = {
  //todo1 定义命名空间
  namespace: 'loginModel',
  //todo2 定义state
  // state:{},
  // todo3 定义动作的创建者 effects中写的是generator函数
  effects: {
    //compParams是组件发送过来的参数
    //call是发送请求
    //put是用于发送effects创建的action给reducers
    *getLogin(compParams, { call }) {
      const result = yield call(service.fetchLogin, compParams.value);
      console.log('result', result);

      if (result.meta.status === 400) {
        message.error('用户名或密码错误');
        return;
      }
      const {
        data: { username, email, mobile, rid, token },
      } = result;
      setCookie('username', username, 7);
      setCookie('email', email, 7);
      setCookie('mobile', mobile, 7);
      setCookie('token', token, 7);
      setCookie('rid', rid, 7);
      history.push('/');
    },
  },
};

export default LoginModel;
